package com.want.evenchat.controller.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.bumptech.glide.load.resource.bitmap.FileDescriptorBitmapDataLoadProvider;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.want.evenchat.R;
import com.want.evenchat.controller.activity.LoginActivity;
import com.want.evenchat.model.Model;

/**
 * Created by Want on 2018/5/4.
 */

public class SettingFragment extends Fragment{

    Button button_setting_exit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity() , R.layout.fragment_setting , null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        button_setting_exit = view.findViewById(R.id.button_setting_exit);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();
    }

    private void initData() {
        //1.在Button上显示当前用户名称
        button_setting_exit.setText("退出登录(" + EMClient.getInstance().getCurrentUser()+")");

        //2.退出登录的逻辑
        button_setting_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        //登录环信服务器退出登录
                        EMClient.getInstance().logout(false, new EMCallBack() {

                            //退出成功
                            @Override
                            public void onSuccess() {

                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {

                                        //关闭DBHelper
                                        Model.getInstance().getDBManager().close();

                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                //更新UI显示
                                                Toast.makeText(getActivity() , "退出成功" , Toast.LENGTH_SHORT).show();
                                            }
                                        });

                                        //回到登录页面
                                        startActivity(new Intent(getActivity() , LoginActivity.class));
                                        getActivity().finish();
                                    }
                                });
                            }

                            //退出失败
                            @Override
                            public void onError(int i, final String s) {
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getActivity() , "退出失败" + s , Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                            //正在退出
                            @Override
                            public void onProgress(int i, String s) {

                            }
                        });
                    }
                });
            }
        });
    }
}
